package server;

import net.sf.json.JSONObject;
import util.AddressHandle;
import util.Browser;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * User: tum0r
 * Package Name: server
 * Time: 2019-03-10 Sunday 01:06:37
 */
public class HistoryServer extends BaseServer {
    public void record(String userID, String recordContent, HttpServletRequest request) {
        String host = request.getRemoteAddr();
        String userAgent = request.getHeader("user-agent");
        Browser browser = new Browser();
        JSONObject browserResult = browser.getBrowser(userAgent);
        record(userID, recordContent, host, browserResult.toString());
    }

    public void record(String userID, String recordContent, String host, String browser) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss (z X)");
        Date date = new Date();
        AddressHandle addressHandle = new AddressHandle();
        sql.update("insert into history values(?, ?, ?, ?, ?, ?)", new Object[]{UUID.randomUUID().toString(), userID, recordContent, addressHandle.handle(host), browser, simpleDateFormat.format(date)});
    }

    public void record(String recordContent, HttpServletRequest request) {
        record("", recordContent, request);
    }
}
